<?php

namespace addons\yunfood\model;

use addons\yunfood\library\ModelTrait;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;

/**
 * 储值记录模型
 */
class FundRecord extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_fund_record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 用户数据
    public function userData()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 行为
    const ACTION_RECHARGE        = 'recharge';
    const ACTION_CONSUME         = 'consume';
    const ACTION_REFUND          = 'refund';
    const ACTION_MANUAL_PLUS     = 'manual_plus';
    const ACTION_MANUAL_MINUS    = 'manual_minus';
    const ACTION_SYSTEM_ROLLBACK = 'system_rollback';


    public static function getActionList()
    {
        return [
            self::ACTION_RECHARGE        => '充值',
            self::ACTION_CONSUME         => '消费',
            self::ACTION_REFUND          => '退款',
            self::ACTION_MANUAL_PLUS     => '手动增加',
            self::ACTION_MANUAL_MINUS    => '手动减少',
            self::ACTION_SYSTEM_ROLLBACK => '系统扣除',
        ];
    }

    public function getActionTextAttr($value, $data)
    {
        $optionList = self::getActionList();

        return $optionList[$data['action']] ?? $data['action'];
    }

    const SOURCE_TYPE_ORDER        = 'order';
    const SOURCE_TYPE_ORDER_REFUND = 'order_refund';
    const SOURCE_TYPE_FUND_ORDER   = 'fund_order';
}
